package utils;

import java.util.List;

public class PageUtil<T> {
    public PageUtil(int totalCount, int pageSize, String currentPage) {
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        initCurrentPage(currentPage);
        initStartIndex();
        initTotalPage();
        initPrePage();
        initNextPage();
    }
    //初始化当前页码
    private void initCurrentPage(String currentPage){
        if(currentPage == null || currentPage.equals("")){
            this.currentPage=1;
        }else{
            this.currentPage = Integer.parseInt(currentPage);
        }
    }
    //初始化每一页第一条记录的起始下标
    private void initStartIndex(){
        this.startIndex=(this.currentPage-1)*this.pageSize;
    }
    //初始化总页数
    private void initTotalPage(){
        this.totalPage=this.totalCount%this.pageSize==0?this.totalCount/this.pageSize:this.totalCount/pageSize+1;
    }
    private void initPrePage(){
        if(this.currentPage ==1){
            this.prePage = 1;
        }else{
            this.prePage=this.currentPage-1;
        }

    }
    private void initNextPage(){
        if(this.currentPage == this.totalPage){
            this.nextPage = this.totalPage;
        }else {
            this.nextPage=this.currentPage+1;
        }

    }

    private int totalCount;//总记录数
    private int pageSize;//页容量
    private int currentPage;//当前页码

    private int startIndex;//每页记录的起始下标
    private int totalPage;//总页数
    private int prePage;//上一页
    private int nextPage;//下一页
    private List<T> list;

    public List<T>   getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getPrePage() {
        return prePage;
    }

    public void setPrePage(int prePage) {
        this.prePage = prePage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }
}
